﻿﻿<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta http-equiv="Pragma" contect="no-cache" />
    <title></title>
    <link href="../source/bootstrap/dist/css/bootstrap.css" rel="stylesheet" />
    <link href="../source/css/bootstrap-select.min.css" rel="stylesheet" />

    <script src="../source/js/common_url.js"></script>
    <script src="../source/js/util.js"></script>
    <script src="../nurse_mgr/js/common_data.js"></script>
    <script src="../source/js/jquery-1.9.1.min.js"></script>
    <script src="../source/bootstrap/dist/js/bootstrap.min.js"></script>
    <script src="../source/js/bootstrap-select.min.js"></script>
    <script src="../source/layer/layer.js"></script>

    <style>
        #c-1 {
            margin-top: 30px;
            margin-left: 100px;
            margin-bottom: 20px;
        }

        #c-2 {
            margin-top: 20px;
            text-align: center;
        }
        #c-3 {
            margin-top: 20px;
            text-align: center;
        }
        #select_queue, #select_doctor{
            width: 200px;
            height: 30px;
            line-height: 30px;
            background-color: #cccccc;
            /*border: none;*/
        }
        #select_doctor{
            width: 216px;
            background-color: #fff;
        }
        .btn{
            width: 50px;
            height: 30px;
            line-height: 17px;
            margin-right: 12px;
        }
        #but_red{
            background: #FF6169;
        }
        #but_green{
            background: #05DB7B;
        }
        #but_yellow{
            background: #FFD03A;
        }

        .on{
            -webkit-box-shadow: 3px 3px #999 inset;
            -moz-box-shadow: 3px 3px #999 inset;
            box-shadow: 3px 3px #999 inset;
        }
        .pretestStage{
            margin: 20px 0 0px 20px;
        }
        .pretestStageTitle{
            width: 20%;
            float: left;
            height: 30px;
            line-height: 30px;
            text-align: right;
            margin:0 10px 5px 0;
        }
        .msg_content{
            margin:3px 0 0 80px;
        }

        .btn:disabled{
            cursor: not-allowed;
            /*filter: alpha(opacity=65);*/
            -webkit-box-shadow: none;
            box-shadow: none;
            opacity: .65;
        }

    </style>
</head>
<body>
<div class="body">
    <div class="body_content">
        <div id="c-1">请选择分诊队列:</div>
        <div id="c-2"><select id="select_queue" onchange="changeQueue();" class="selectpicker"></select></div>
        <div id="c-3">
            <select id="select_doctor" onchange="changeDoctor();" class="selectpicker"></select>
        </div>
    </div>
    <div class="pretestStage" id="color_buttons">
        <span class="pretestStageTitle">预检选项:</span>
        <button id="but_red" class="btn" onclick="btnRed();" ></button>
        <button id="but_yellow" class="btn" onclick="btnYellow();"></button>
        <button id="but_green" class="btn" onclick="btnGreen();"></button>
        <div class="msg_content" id="msg-for-txt_pretestStage"></div>
    </div>
</div>
</body>
</html>
<script type="text/javascript">
    var _greenPipePatients = STSUrl + "/QueuingOperate/greenChannelOperate";
    var _yellowPipePatients = STSUrl + "/QueuingOperate/yellowPretestStage";
    var _checkBack = STSUrl+"/QueuingOperate/checkBack";
    //复诊时默认医生/可选医生
    var getDoctorByQueueIdUrl= QueueCallUrl+ "/QcQueue/getNotPageDoctorByQueueId";
    //根据队列ID查询队列下关联医生列表 http://yahua.tpddns.cn:8090/pages/viewpage.action?pageId=4325601
    var queryDoctorListByQueueUrl = PDJH + "/QcQueue/getQueueInfoByQueueId";
    //判断医生是否在线
    var _doctorStatusUrl = CallNumberUrl+"/CallDevice/getDoctorLoginStatus";
    var _deptId = GetQueryString("deptId");     // 选中系统所在的科室id
    var _queueKey = GetQueryString("queueKey");   //
    var _systemId = GetQueryString("systemId"); // 系统id
    var _hospitalId = getHospitalId();       // 医院id
    var color_status = GetQueryString("pretestStage"); //预检状态
    var fenzFzType = GetQueryString("fenzFzType");//区分初诊和复诊类型
    var btnType ="";
    var ret = false;

    $(function () {
        //判断是否为预检
        var colorStatus = localStorage["colorStatus"];
        if (colorStatus == 1) {
            $("#color_buttons").show();
        } else {
            $("#color_buttons").hide();
            btnType = 3;
        }
        pretestStageStatus();
        $.ajax({
            url: _getQueueForTriage,
            type: "get",
            data: {
                deptId: _deptId,
                sysId: _systemId
            },
            success: function (data) {
                var result = JSON.parse(data);
                if (result.status == 0 && result.data != null && result.data.length > 0) {
                    var content = "";
                    for (var i = 0; i < result.data.length; i++) {
                        content += "<option value='" + result.data[i].queueId;
                        content += "' data-queueKey='"+result.data[i].queueKey;
                        content += "' data-relationType='"+result.data[i].relationType;
                        content += "' data-queueStatus='"+result.data[i].queueStatus;
                        // content += "' data-queueKey='"+result.data[i].queueKey;
                        if(_queueKey == result.data[i].queueKey){
                            content += "' selected='selected'>";
                        } else {
                            content += "'>";
                        }
                        content += result.data[i].queueName;
                        content += "</option>";
                    }

                    $("#select_queue").html(content);
                    $('#select_queue').selectpicker('refresh');
                    $('#select_queue').selectpicker('render');
                    var relationType = $("#select_queue").find("option:selected").attr("data-relationType");
                    var queueStatus = $("#select_queue").find("option:selected").attr("data-queueStatus");
                    var _queueId = $("#select_queue").val();
                    if (relationType==1){
                        var msg = "普通队列！";
                        doctorNameInfo(_queueId, relationType, msg);
                        return;
                    }
                    if(queueStatus == 2) {
                        if (fenzFzType == "fz") {//复诊
                            var msg = "该患者初诊医生已不在线！";
                            doctorNameInfo(_queueId, relationType, msg);
                        } else {
                            $("#c-3").hide();
                        }
                      var index =parent.layer.alert("您所选队列已离线，请为其指定其他队列或继续分诊。", {
                            closeBtn: 0,
                            icon: 7,
                            zIndex: 19892002,
                        }, function () {
                           parent.layer.close(index);
                        })
                    } else {
                        if (fenzFzType == "fz") {//复诊
                            var msg = "该患者初诊医生已不在线！";
                            doctorNameInfo(_queueId, relationType, msg);
                        } else {
                            $("#c-3").hide();
                        }
                    }
                } else {
                    // 关闭当前窗口
                    var index = parent.layer.getFrameIndex(window.name);
                    parent.layer.close(index);
                    layer.alert(result.desc, { icon: 2 });
                }
            },
            error: function (jqXHR, textStatus, errorThrown) {
                console.log(textStatus);
                console.log(errorThrown);
                layer.msg("操作失败!", { icon: 2 });
            }
        });
    });
    // 切换队列 重置医生
    function changeQueue() {
        var queueId =$("#select_queue").val();
        $.ajax({
            url: queryDoctorListByQueueUrl,
            type: 'get',
            data: {
                queueId:queueId
            },
            beforeSend: function () {
                layer.msg('查询中...',{time:1000});
            },
            complete: function () {
                // layer.closeAll();
            },
            success: function (d) {
                console.log(d);
                if (JSON.parse(d).status == '0') {
                    var dataInfo = JSON.parse(d).data;
                    var relationType = dataInfo.relationType;//队列类型 1:普通队列  2：专家队列
                    var queueDoctorInfo = dataInfo.queueDocInfo;
                    var queueStatus = dataInfo.queueStatus;//0：在线  1：暂停   2：离线
                    var queueId = $("#select_queue").val();
                    if(queueStatus == 2) {
                        var index = parent.layer.alert("您所选队列已离线，请为其指定其他队列或继续分诊。", {
                            closeBtn: 0,
                            icon: 7,
                            zIndex: 19892002,
                            cancel: function () {
                            },
                            yes: function () {
                                if(fenzFzType == "fz"){
                                    var msg = "该患者初诊医生已不在线！";
                                    doctorNameInfo(queueId,relationType,msg);
                                } else{
                                    $("#c-3").hide();
                                }
                                parent.layer.close(index);
                            }
                        })
                    } else {
                        if(fenzFzType == "fz"){
                            var msg = "该患者初诊医生已不在线！";
                            doctorNameInfo(queueId,relationType,msg);
                        } else{
                            $("#c-3").hide();
                        }
                    }
                } else{
                    parent.layer.alert(JSON.parse(d).desc);
                }
            }
        });
    }
    function changeDoctor(){
        var queueId = $('#select_queue option:selected').val();
        var _docId = $('#select_doctor option:selected').val();
        var _loginStatus = $('#select_doctor option:selected').attr("data-status");
        $.ajax({
            url: _doctorStatusUrl,
            type: "get",
            data: {
                hospitalId: _hospitalId,
                queueId: queueId,
                doctorId: _docId
            },
            success: function (data) {
                var result = JSON.parse(data);
                var msg = "该医生不在线!";
                if (result.data == 0) { //医生离线
                    var doctorName = $('#select_doctor option:selected').text();
                    if(doctorName != "全部医生"){
                        if(_loginStatus == 2){
                            msg = "该患者初诊医生已不在线！";
                        } else{
                            msg = "该医生不在线!";
                        }
                        layer.msg(msg,{time:2000,zIndex:19892000,anim: 5});
                    }
                }
            },
            error: function (jqXHR, textStatus, errorThrown) {
                layer.alert("操作失败!", { icon: 2 });
            }
        });
    }
    //患者复诊选择初诊时的医生，该医生在线时显示初诊医生，不在线时显示全部
    function doctorNameInfo(queueId,relationType,msg) {
        var _doctorHtml = "";
        var countDoctor = 0;
        if(relationType == 1){
            _doctorHtml = "<option value=''>全部医生</option>";
            countDoctor = 1;
        }
        var _patientKey = GetQueryString("patientKey"); //
        $.ajax({
            url: getDoctorByQueueIdUrl,
            data: {
                deptId: _deptId,
                hospitalId: _hospitalId,
                queueId: queueId,
                patientKey:_patientKey
            },
            type: 'get',
            beforeSend: function () {
                layer.msg('查询中...',{time: 1000});
            },
            complete: function () {
                // layer.closeAll();
            },
            success: function (d) {
                if (JSON.parse(d).status == '0') {
                    var dataInfo = JSON.parse(d).data;
                    var lengthList = dataInfo.length;
                    countDoctor += lengthList;
                    for (var i = 0; i < lengthList; i++) {
                        var _doctorId = dataInfo[i].doctorId;
                        var _doctorName = dataInfo[i].doctorName;
                        var _loginStatus = dataInfo[i].loginStatus;//初诊医生展示字段  0：在线 1：暂停  2：离线
                        //医生不在线提示信息
                        if( _loginStatus == 2 ){
                            layer.msg(msg,{time:2000,zIndex:19892000,anim: 5});
                        }
                        if (_loginStatus == 1 ||_loginStatus == 0) {
                            _doctorHtml += "<option selected data-status='" + _loginStatus + "' value='" + _doctorId + "'>" + _doctorName + "</option>";
                        }
                        else {
                            _doctorHtml += "<option data-status='" + _loginStatus + "' value='" + _doctorId + "'>" + _doctorName + "</option>";
                        }
                    }

                    //判断队列医生人数
                    if( countDoctor == 1 ){
                        $("#select_doctor").prop("disabled", true);
                        $("#select_doctor").css({ "color": "#B8B7B8" });
                    } else {
                        $("#select_doctor").prop("disabled", false);
                        $("#select_doctor").css({ "color": "#000" });
                    }
                    //医生下拉条 赋值
                    $("#select_doctor").html(_doctorHtml);
                    $('#select_doctor').selectpicker('refresh');
                    $('#select_doctor').selectpicker('render');
                }
            }
        });
    }

    //选择队列医生后校验是否复诊
    function checkBack(callback){
        var ids = GetQueryString("ids");
        var loginId = getLoginUserId();
        var triageId = getDeviceId();
        var queueKey = $("#select_queue option:selected").attr("data-queueKey");
        var queueId = $("#select_queue").val();
        var patientKey = GetQueryString("patientKey");
        var fenzFzSignId = GetQueryString("fenzFzSignId");//区分复诊页面和未分诊页面signId传值
        if( fenzFzSignId == "fzY"){//分诊患者非未分诊队列
            $.ajax({
                url: _checkBack,
                type: "post",
                async: false,
                data: {
                    id: ids,
                    loginId: loginId,
                    hospitalId: _hospitalId,
                    triageId: triageId,
                    queueKey:queueKey,
                    queueId:queueId,
                    patientKey:patientKey
                }, success: function (d) {
                    var jsonObj = JSON.parse(d);
                    if (jsonObj.status == 0) {
                        var signIds = jsonObj.data;
                        setTriageQueue(signIds,callback);
                    } else {
                        //异常的提示信息
                        layer.alert(jsonObj.desc);
                    }
                }
            })
        } else {
            var signIds = GetQueryString("signId");
            setTriageQueue(signIds,callback);
        }
    }


    function setTriageQueue(signIds,callback) {
        var queueKey = $("#select_queue option:selected").attr("data-queueKey");
        var loginId = getLoginUserId();
        var colorStatus = localStorage["colorStatus"];

        var doctorId =$('#select_doctor option:selected') .val();
        var doctorName =$('#select_doctor option:selected') .text();
        if(doctorName == "全部医生"){
            doctorName = "";
            doctorId = "";
        }
        if (colorStatus == 1) {
            if ( btnType == "" ){
                $("#msg-for-txt_pretestStage").html("请选择预检选项！");
                $("#msg-for-txt_pretestStage").css({ "color": "red", "font-weight": "bold" });
                return ;
            }
        }
        // 发送ajax请求转载
        $.ajax({
            url: _triagePatients,
            type: "post",
            async: false,
            data: {
                hospitalId: _hospitalId,
                signIds: signIds,
                queueKey: queueKey,
                editor: loginId,
                pretestStage:btnType,
                doctorId: doctorId,
                doctorName: doctorName
            },
            success: function (data) {
                var res = JSON.parse(data);
                console.log(callback)
                callback&&callback(res)
            },
            error: function (jqXHR, textStatus, errorThrown) {
                callback&&callback({status:10086,desc:'操作失败'})
            }
        });
    }

    //点击红色按钮
    function btnRed(){
        btnType = 1;
        $(".btn").removeClass('on');
        $("#but_red").addClass('on');
        $("#msg-for-txt_pretestStage").html("");
    }
    //点击黄色按钮
    function btnYellow(){
        btnType = 2;
        $(".btn").removeClass('on');
        $("#but_yellow").addClass('on');
        $("#msg-for-txt_pretestStage").html("");
    }
    //点击绿色按钮
    function btnGreen(){
        btnType = 3;
        $(".btn").removeClass('on');
        $("#but_green").addClass('on');
        $("#msg-for-txt_pretestStage").html("");
    }
    //复诊时按钮默认效果
    function pretestStageStatus(){
        if (color_status == 1){
            btnType =1;
            $(".btn").removeClass('on');
            $("#but_red").addClass("on");
            $("#but_yellow").attr('disabled', "disabled");
            $("#but_green").attr('disabled', "disabled");
        }else if(color_status == 2){
            btnType =2;
            $(".btn").removeClass('on');
            $("#but_yellow").addClass("on");
            $("#but_red").attr('disabled', "disabled");
            $("#but_green").attr('disabled', "disabled");
        }else if(color_status == 3){
            btnType = 3;
            $("#but_red").attr('disabled', "disabled");
            $("#but_yellow").attr('disabled', "disabled");
            $(".btn").removeClass('on');
            $("#but_green").addClass("on");
        }
    }

</script>
